package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.app.utils.UserContext;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.utils.JSONResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/users")
@Api(value = "用户资源",description = "用户资源控制器")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategySunCommentService strategySunCommentService;
    @Autowired
    private IStrategyReplyCommentService strategyReplyCommentService;

    @PostMapping
    @ResponseBody
    @ApiOperation(value = "注册功能",notes = "其实就是新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "昵称",name = "nickName",dataType = "String",required = true),
            @ApiImplicitParam(value = "邮箱",name = "email",dataType = "String",required = true),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="用户注册成功")
    })
    public Object register(User user){
        JSONResult result = new JSONResult();
        try {
            userService.register(user);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping("/check")
    @ResponseBody
    public boolean check(String email){
        User user = userMapper.selectUserByEmailAndPassword(email, null);
        if(user==null){ return true; }
        return false;
    }

    /*
    * 更新用户
    *请求资源:users
    *请求方式:put
    *请求参数:user
    *请求结果:json
    *
    * */
    @PutMapping
    @ResponseBody
    public Object updateUser(User user){
        JSONResult result = new JSONResult();
        userMapper.updateByPrimaryKey(user);
        User t = userMapper.selectByPrimaryKey(user.getId());

        //修改session中的数据
        UserContext.setCurrentUser(t);
        //设置json的obj属性
        result.setObj(t);
        return result;
    }



    /*我的游记接口设计
    * 1.请求方式:get
    * 2.请求资源:/users/{id}/travels
    * 3.请求参数:id,qo对象
    * 4.返回对象 json--PageInfo
    * */
    @GetMapping("/{userId}/travels")
    @ResponseBody
    public Object getTravel(@ModelAttribute("qo")TravelQueryObject qo){
        return travelService.query(qo);
    }


    @GetMapping("/{userId}/likeMe")
    @ResponseBody
    public Object getLikeMe(@ModelAttribute("qo")TravelQueryObject qo){
        qo.setOrderBy("lt.createTime");
        return travelService.queryForLikeMe(qo);
    }
    @GetMapping("/{userId}/likeMyComment")
    @ResponseBody
    public Object likeMyComment(@ModelAttribute("qo")TravelQueryObject qo){
        qo.setOrderBy("lt.createTime");
        return strategyCommentService.queryForLikeMe(qo);
    }
//根据用户id获取用户信息
    @GetMapping
    @ResponseBody
    public User getUserByUserId( Long userId){
        return userService.getUserByUserId(userId);
    }
    //改变用户粉丝数,关注数
    @PutMapping("/changeFans")
    @ResponseBody
    public boolean changeFans( Long userId,Long authorId){

        return  userService.changeFans(userId,authorId);
    }
    //判读用户与作者是否存在关系
    @GetMapping("/relations")
    @ResponseBody
    public boolean relations( Long userId,Long authorId){
        return  userService.relations(userId,authorId);
    }
    //判断用户是否与游记存在关系
    @GetMapping("/travelRelations")
    @ResponseBody
    public boolean travelRelations( Long userId,Long travelId){
        return  userService.travelRelations(userId,travelId);
    }
    //判断用户是否与咨询存在关系
    @GetMapping("/newsRelations")
    @ResponseBody
    public boolean newsRelations( Long userId,Long newsId){
        return  userService.newsRelations(userId,newsId);
    }

    //根据用户id获取用户游记收藏
    @GetMapping("/travelCollections")
    @ResponseBody
    public Object getTravelCollections( Long userId){
        return userService.getTravelCollections(userId);
    }
    //根据用户id获取用户游记收藏
    @GetMapping("/newsCollections")
    @ResponseBody
    public Object getNewsCollections( Long userId){
    return userService.getNewsCollections(userId);
    }

    //改变用户游记收藏
    @PutMapping("/changeTravelCollections")
    @ResponseBody
    public boolean changeTravelCollections( Long userId,Long travelId){

        return  userService.changeTravelCollections(userId,travelId);
    }
    //改变用户咨询收藏
    @PutMapping("/changeNewsCollections")
    @ResponseBody
    public boolean changeNewsCollections( Long userId,Long newsId){

        return  userService.changeNewsCollections(userId,newsId);
    }



    @GetMapping("/{userId}/AddMe")
    @ResponseBody
    public Object AddMe(@ModelAttribute("qo")QueryObject qo){
        qo.setOrderBy("ssc.createTime desc");
        return strategySunCommentService.queryForAddMe(qo);
    }
    @GetMapping("/{userId}/CommentMe")
    @ResponseBody
    public Object CommentMe(@ModelAttribute("qo")TravelQueryObject qo){
        qo.setOrderBy("src.createTime desc");
        return strategyReplyCommentService.selectForCommentMine(qo);
    }
}
